<?php
/**
 *	KLF Framework
 *
 *	Written by Kristian Oye <kristianoye@gmail.com> on Nov 19, 2011
 *	Copyright (C) 2010-2011
 */
namespace KLF\System\Cache
{
	use KLF\System\Cache\KCache;
	use KLF\System\Cache\ICache;
	use KLF\System\Cache\ICacheable;
	use KLF\System\Errors\KFeatureException;
	use KLF\System\Errors\KArgumentException;
	
	/**
	 *  Class KMemcache extends KCache
	 *
	 *  Framework implementation/wrapper for Memcache
	 *
	 *  @package KLF\System\Cache\Memcache
	 *  @access public
	 *  @author Kristian Oye <kristianoye@gmail.com>
	 *  @since 0.1
	 *  
	 *  @see KMemcache::SetValue()
	 *  @see KMemcache::GetValues();
	 *
	 *  @method KMemcache NewInstance
	 */
	class KWincache extends KCache
	{
		/**
		 * Constructs a new KWincache object.
		 */
		public function __construct()
		{
			if (!extension_loaded('wincache')) {
				throw new KFeatureException('wincache');
			}
		}
		
		/**
		 * Adds a value to the cache.
		 * 
		 * @param string $key The key associated with the value.
		 * @param mixed $val
		 * @param integer $expires
		 * @param integer $flag
		 * 
		 * @return boolean True if successfully added, false if it failed.
		 */
		public function AddValue($key, $val, $expires=0)
		{
			return wincache_ucache_add($key, $val, $expires);
		}
		
		/**
		 * Deletes values from the cache by using one or more cache keys.
		 *
		 * @author: Kristian Oye <kristianoye@gmail.com>
		 * @since 0.1
		 * @return integer Returns the number of deleted keys.
		*/
		public function DeleteValues()
		{
			if (count($args) == 0) {
				throw new KArgumentException("Insufficient arguments to DeleteValues()");
			}
			foreach($args as $keyName) {
				wincache_ucache_delete($keyName);
			}
			return $this;
		}
		
		/**
		 * Clears all the values out of the memcache.
		 * 
		 * @see KLF\System\Cache.ICache::Flush()
		 */
		public function Flush()
		{
			wincache_ucache_clear();
			
			return $this;
		}
		
		/**
		 * Returns a single value from the cache.
		 * 
		 * @param string $key
		 * @see KLF\System\Cache.ICache::GetValue()
		 * @return mixed
		 */
		public function GetValue($key)
		{
			return wincache_ucache_get($key);
		}
		
		/**
		 * Returns a set of one or more values from the cache.
		 * 
		 * @see KLF\System\Cache.ICache::GetValues()
		 */
		public function GetValues()
		{
			$args = func_get_args();
			$result = array();
			
			if (count($args) == 0) {
				throw new KArgumentException("Insufficient arguments to GetValues()");
			}
			return count($args) === 1 ? 
				wincache_ucache_get($args[0]) : 
				wincache_ucache_get($args);
		}
		
		/**
		* Initialize the fake cache object.
		 *
		* @see KLF\System\Cache.ICache::Initialize()
		*/
		public function Initialize()
		{
		}
		
		/**
		 * Add or update a value in the cache.
		 * 
		 * @param $key The key associated with the value and used to retrieve it.
		 * @param $val The value to store in the cache.
		 * @param $expires The maximum time the value should live in the cache.
		 * 
		 * @see KLF\System\Cache.ICache::SetValue()
		 */
		public function SetValue($key, $val, $expires = 0)
		{
			wincache_ucache_set($key, $val, $expires);
			
			return $this;
		}
	}
}
